package com.example.demo.bizhan.reflect;

import org.junit.Test;

import java.lang.reflect.Field;

import static java.lang.System.out;

/**
 * @description: xxx
 * @time: 2020-10-26 22:02
 * @author: baojinlong
 **/
public class FieldDemoTest {


    @Test
    public void test02() throws Exception {
        Class<?> studentClazz = Class.forName("com.example.demo.bizhan.reflect.Student");
        Student student = (Student) studentClazz.newInstance();
        Field sex = studentClazz.getDeclaredField("sex");
        // Class com.example.demo.bzhang.reflect.FieldDemo can not access a member of class com.example.demo.bzhang.reflect.Student with modifiers "private"
        // sex.set(student, "男");
        if (!sex.isAccessible()) {
            sex.setAccessible(true);
        }
        sex.set(student, "男");
        out.println(student);

    }

    @Test
    public void test01() throws Exception {
        Class<?> studentClazz = Class.forName("com.example.demo.bizhan.reflect.Student");
        Field[] declaredFields = studentClazz.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            out.println(declaredField.getType() + " : " + declaredField.getName());
        }
        Field sex = studentClazz.getDeclaredField("sex");
        out.println(sex.getName() + " : " + sex.getType());
    }
}
